﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WPFNote.Utilities.Common
{
    public class FilesHelper
    {
        /// <summary>
        /// 新建并写入文本数据到文件
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="content"></param>
        /// <param name="isAppend">内容是否追加到文件最后</param>
        public static void CreateNewFile(string filePath, string content, bool isAppend = false)
        {
            var path = Path.GetDirectoryName(filePath);
            if (string.IsNullOrEmpty(path)) return;

            if (!System.IO.Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            if (!System.IO.File.Exists(filePath))
            {
                var stream = System.IO.File.Create(filePath);
                stream.Close();
                stream.Dispose();
            }

            if (isAppend)
            {
                File.AppendAllText(filePath, content);
            }
            else
            {
                File.WriteAllText(filePath, content);
            }
        }

        /// <summary>
        /// 新建并写入字节数组数据到文件
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="content"></param>
        public static void CreateNewFile(string filePath, byte[] content)
        {
            var path = Path.GetDirectoryName(filePath);
            if (string.IsNullOrEmpty(path)) return;

            if (!System.IO.Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            if (!System.IO.File.Exists(filePath))
            {
                var stream = System.IO.File.Create(filePath);
                stream.Close();
                stream.Dispose();
            }
            File.WriteAllBytes(filePath, content);
        }

        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="filePath"></param>
        public static bool DeleteFile(string filePath)
        {
            if (!System.IO.File.Exists(filePath))
            {
                return false;
            }
            File.Delete(filePath);
            return true;
        }

        /// <summary>
        /// 添加字符串到TXT文本
        /// </summary>
        /// <param name="content"></param>
        /// <param name="folder">存放文件夹，格式：aaa/bbb/ccc/，或aaa\\bbb\\ccc\\</param>
        public static void AppendTxt(string content, string folder = "Logs")
        {
            var basePath = System.AppDomain.CurrentDomain.BaseDirectory.Trim('\\') + "\\";
            var filePath = (basePath + folder.Replace("/", "\\")).Trim('\\') + "\\" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
            var path = Path.GetDirectoryName(filePath);
            if (string.IsNullOrEmpty(path)) return;

            if (!System.IO.Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            if (!System.IO.File.Exists(filePath))
            {
                var stream = System.IO.File.Create(filePath);
                stream.Close();
                stream.Dispose();
            }
            using (var writer = new StreamWriter(filePath, true))
            {
                writer.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff") + ": " + content);
            }
        }
    }
}
